﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GUI
{
    [Serializable()]
    public class BaseControl
    {
        private static ContentManager _content;

        [XmlIgnore()]
        public ContentManager Content
        {
            set { _content = value; }
            get { return _content; }
        }

        #region Position

        private Vector2 _position;
        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }

        #endregion
        
        #region Textures

        private Texture2D _inactive,_clicked,_mouseover;
        private string _strinactive, _strclicked, _strmouseover;

        [XmlIgnore()]
        public Texture2D Inactive
        {
            get { return _inactive; }
            set { _inactive = value; }
        }

        [XmlIgnore()]
        public Texture2D Clicked
        {
            get { return _clicked; }
            set { _clicked = value; }
        }

        [XmlIgnore()]
        public Texture2D MouseOver
        {
            get { return _mouseover; }
            set { _mouseover = value; }
        }

        public string InactiveAsset
        {
            get { return _strinactive; }
            set { 
                _strinactive = value;
                _inactive = _content.Load<Texture2D>(_strinactive);
                }
        }

        public string ClickedAsset
        {
            get { return _strclicked; }
            set
            {
                _strclicked = value;
                _clicked = _content.Load<Texture2D>(_strclicked);
            }
        }

        public string MouseOverAsset
        {
            get { return _strmouseover; }
            set
            {
                _strmouseover = value;
                _mouseover = _content.Load<Texture2D>(_strmouseover);
            }
        }

        #endregion

        #region States

        private States _state;
        public States State
        {
            get { return _state; }
            set { _state = value; }
        }

        [Serializable()]
        public enum States
        {
            Inactive = 0,
            Clicked,
            MouseOver
        };

        #endregion

        public BaseControl()
        { }

        public virtual void Initialize(ContentManager cm)
        {
            _content = cm;
        }

        public virtual void Update()
        {
            MouseState ms = Mouse.GetState();

            if (ms.X > _position.X && ms.X < (_position.X + _inactive.Width) &&
                    ms.Y > _position.Y && ms.Y < (_position.Y + _inactive.Height))
            {
                State = States.MouseOver;

                if (ms.LeftButton == ButtonState.Pressed)
                {
                    State = States.Clicked;
                }

            }
            else
            {
                State = States.Inactive;
            }
            
        }

        public virtual void Draw(SpriteBatch sb)
        {
            switch (State)
            {
                case States.Inactive:
                    {
                        sb.Draw(_inactive, _position, Color.White);
                        break;
                    }
                case States.Clicked:
                    {
                        sb.Draw(_clicked,_position,Color.White);
                        break;
                    }
                case States.MouseOver:
                    {
                        sb.Draw(_mouseover, _position, Color.White);
                        break;
                    }
            }
        }
    }
}
